package v1

import (
	"gitee.com/daka1004/devops-runner/constant"
	"gitee.com/daka1004/devops-runner/dto"
	"github.com/gin-gonic/gin"
)

func (b *BaseApi) Executebak(c *gin.Context) {

	var req dto.RunnerPipeline
	if err := c.ShouldBindJSON(&req); err != nil {
		NewResponse(c).error(constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err).Json()
		return
	}

	if _, _, err := runnerService.Execute(req); err != nil {
		NewResponse(c).error(constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err).Json()
		return
	}
	NewResponse(c).Success(map[string]interface{}{}).Json()
}
func (b *BaseApi) Execute(c *gin.Context) {

	var req dto.RunnerPipeline
	if err := c.ShouldBindJSON(&req); err != nil {
		NewResponse(c).error(constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err).Json()
		return
	}
	req.ExecuteID = c.Param("executeId")
	_, logdata, err := runnerService.Execute(req)
	if err != nil {
		NewResponse(c).error(constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err).Json()
		return
	}
	NewResponse(c).Success(logdata).Json()
}
func (b *BaseApi) GetNodeLog(c *gin.Context) {

	var req dto.RunnerPipeline
	if err := c.ShouldBindJSON(&req); err != nil {
		NewResponse(c).error(constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err).Json()
		return
	}
	req.ExecuteID = c.Param("executeId")
	_, logdata, err := runnerService.GetNodeLog(req)
	if err != nil {
		NewResponse(c).error(constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err).Json()
		return
	}
	NewResponse(c).Success(logdata).Json()
}
func (b *BaseApi) DeleteNodeLog(c *gin.Context) {

	var req dto.RunnerPipeline
	if err := c.ShouldBindJSON(&req); err != nil {
		NewResponse(c).error(constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err).Json()
		return
	}
	req.ExecuteID = c.Param("executeId")
	_, logdata, err := runnerService.DeleteNodeLog(req)
	if err != nil {
		NewResponse(c).error(constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err).Json()
		return
	}
	NewResponse(c).Success(logdata).Json()
}
